<?php

namespace App\Task\Video;

use App\Cache\InviteVideoCache;
use App\Constants\InviteVideoCode;
use App\Model\InviteVideoLog;
use App\Service\ServiceTrait\InviteVideoCheckTrait;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Hyperf\Crontab\Annotation\Crontab;

/**
 * 检测邀约忙时的邀约视频
 * @Crontab(name="CloseInviteVideoTask", rule="*\/1 * * * * *", callback="execute", memo="检测邀约忙时的邀约视频",singleton=false)
 */
class CloseInviteVideoTask
{
    use InviteVideoCheckTrait;

    /**
     * 定时任务
     * @return void
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function execute()
    {
        $runList = InviteVideoLog::getInviteStatusData();

        if (empty($runList)){
            log_info('CloseInviteVideoTask:info:NoDataList');
            return;
        }

        array_walk($runList, function ($oneInfo) {
            if (time() < (strtotime($oneInfo['created_at']) + (InviteVideoCode::AUTO_CANCEL * 60))){
                return;
            }

            $maleId = $oneInfo['male_id'];
            $femaleId = $oneInfo['female_id'];

            $this->reloadData($maleId,$femaleId,InviteVideoLog::STATUS_NO_ANSWER);
        });
    }
}
